# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 16, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
# SUBSET: ONLY MATCHED PARTISANS. 
our.data = subset(our.data, programmatic==1)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# IDEOLOGICAL SELF-IDENTIFICATION

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# BREAKDOWN, 1984
our.means.1984 = svytable(~ as.factor(ideology) + republican, 
                          design = subset(our.design, year==1984))
our.means.1984 = prop.table(our.means.1984, 2)
our.means.1984 = as.data.frame(our.means.1984)
names(our.means.1984)[1] = "value"
# BREAKDOWN, 2020
our.means.2020 = svytable(~ as.factor(ideology) + republican, 
                          design = subset(our.design, year==2020))
our.means.2020 = prop.table(our.means.2020, 2)
our.means.2020 = as.data.frame(our.means.2020)
names(our.means.2020)[1] = "value"
# BIND THE TWO DATASETS
means.data = rbind(our.means.1984, our.means.2020)
levels(means.data$value) = c("Extremely \nliberal", "Liberal", "Slightly \nliberal", "Slightly \ncons.", 
                             "Cons.", "Extremely \ncons.")
# YEAR 
means.data$year = c(rep(1984, 12), rep(2020, 12))
# VARIABLE NAME 
means.data$variable.name = c(rep("Ideological \nself-identification", 24))
# PARTISANSHIP 
means.data$republican = car::recode(means.data$republican, "1 = 'Republicans'; 0 = 'Democrats'")
# DATASET NAME 
means.data.ideology = means.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GOVERNMENT GUARANTEED JOBS

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# BREAKDOWN, 1984
our.means.1984 = svytable(~ as.factor(govjobs) + republican, 
                          design = subset(our.design, year==1984))
our.means.1984 = prop.table(our.means.1984, 2)
our.means.1984 = as.data.frame(our.means.1984)
names(our.means.1984)[1] = "value"
# BREAKDOWN, 2020
our.means.2020 = svytable(~ as.factor(govjobs) + republican, 
                          design = subset(our.design, year==2020))
our.means.2020 = prop.table(our.means.2020, 2)
our.means.2020 = as.data.frame(our.means.2020)
names(our.means.2020)[1] = "value"
# BIND THE TWO DATASETS
means.data = rbind(our.means.1984, our.means.2020)
levels(means.data$value) = c("Gov. should\n provide job\n and standard \nof living", "2", "3", "4 \n(includes \nHTMA)", "5", 
                             "6", "Let people\n get ahead\n on own")
# YEAR 
means.data$year = c(rep(1984, 14), rep(2020, 14))
# VARIABLE NAME 
means.data$variable.name = c(rep("Government \nguaranteed jobs", 28))
# PARTISANSHIP 
means.data$republican = car::recode(means.data$republican, "1 = 'Republicans'; 0 = 'Democrats'")
# DATASET NAME 
means.data.govjobs = means.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GOVERNMENT AID TO BLACKS

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# BREAKDOWN, 1984
our.means.1984 = svytable(~ as.factor(aidblacks) + republican, 
                          design = subset(our.design, year==1984 & whiteNH==1))
our.means.1984 = prop.table(our.means.1984, 2)
our.means.1984 = as.data.frame(our.means.1984)
names(our.means.1984)[1] = "value"
# BREAKDOWN, 2020
our.means.2020 = svytable(~ as.factor(aidblacks) + republican, 
                          design = subset(our.design, year==2020 & whiteNH==1))
our.means.2020 = prop.table(our.means.2020, 2)
our.means.2020 = as.data.frame(our.means.2020)
names(our.means.2020)[1] = "value"
# BIND THE TWO DATASETS
means.data = rbind(our.means.1984, our.means.2020)
levels(means.data$value) = c("Gov. should \nhelp Blacks", "2", "3", "4 \n(includes \nHTMA)", "5", 
                             "6", "Blacks should\n help\n themselves")
# YEAR 
means.data$year = c(rep(1984, 14), rep(2020, 14))
# VARIABLE NAME 
means.data$variable.name = c(rep("Government aid to Blacks\n (just non-Hispanic White respondents)", 28))
# PARTISANSHIP 
means.data$republican = car::recode(means.data$republican, "1 = 'Republicans'; 0 = 'Democrats'")
# DATASET NAME 
means.data.aidblacks = means.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# ABORTION

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# BREAKDOWN, 1984
our.means.1984 = svytable(~ as.factor(abortion) + republican, 
                          design = subset(our.design, year==1984))
our.means.1984 = prop.table(our.means.1984, 2)
our.means.1984 = as.data.frame(our.means.1984)
names(our.means.1984)[1] = "value"
# BREAKDOWN, 2020
our.means.2020 = svytable(~ as.factor(abortion) + republican, 
                          design = subset(our.design, year==2020))
our.means.2020 = prop.table(our.means.2020, 2)
our.means.2020 = as.data.frame(our.means.2020)
names(our.means.2020)[1] = "value"
# BIND THE TWO DATASETS
means.data = rbind(our.means.1984, our.means.2020)
levels(means.data$value) = c("Abortion should\n always be\n legal", "Abortion if \nneed established", 
                             "Illegal with\n exceptions\n (rape, incest, \nlife of mother)", 
                             "Abortion should \nalways \nbe illegal") 
# YEAR 
means.data$year = c(rep(1984, 8), rep(2020, 8))
# VARIABLE NAME 
means.data$variable.name = c(rep("Abortion", 16))
# PARTISANSHIP 
means.data$republican = car::recode(means.data$republican, "1 = 'Republicans'; 0 = 'Democrats'")
# DATASET NAME 
means.data.abortion = means.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# MERGE DATA
new.data = rbind(means.data.ideology, means.data.govjobs, means.data.aidblacks, means.data.abortion)
new.data$variable.name = factor(new.data$variable.name, levels = c("Ideological \nself-identification", 
                                                                   "Government \nguaranteed jobs", 
                                                                   "Government aid to Blacks\n (just non-Hispanic White respondents)", 
                                                                   "Abortion"))
new.data$year = factor(new.data$year)
new.data$value = factor(new.data$value, levels = c("Extremely \nliberal", "Liberal", 
                                                   "Slightly \nliberal",
                                                   "Slightly \ncons.", "Cons.", 
                                                   "Extremely \ncons.", 
                                                   "Gov. should\n provide job\n and standard \nof living",
                                                   "Gov. should \nhelp Blacks", 
                                                   "2", 
                                                   "3", 
                                                   "4 \n(includes \nHTMA)", 
                                                   "5",
                                                   "6", 
                                                   "Let people\n get ahead\n on own", 
                                                   "Blacks should\n help\n themselves",
                                                   "Abortion should\n always be\n legal", "Abortion if \nneed established", 
                                                   "Illegal with\n exceptions\n (rape, incest, \nlife of mother)", 
                                                   "Abortion should \nalways \nbe illegal"))
new.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# FIGURE 16: MATCHED PARTISANS IN 1984 AND 2020: DISTRIBUTIONS OF IDEOLOGICAL SELF-
# IDENTIFICATION AND DOMAIN-SPECIFIC ISSUE POSITIONS. 
library(tidyverse)
library(ggh4x)
our.plot = ggplot(new.data, aes(x = value, y = Freq, group = year,
                                color =  year)) +
  # labs(color = "Partisanship") +
  geom_line(aes(color = year), linewidth = 1) +
  ylab("Proportion \n") + xlab("") +
  scale_color_grey(start = 0.8, end = 0.4) +
  facet_nested_wrap(~variable.name + republican, nrow = 4, scales = "free") +
  theme_minimal() + 
  # ggtitle("Matched Partisans in 1984 and 2020: Distributions of Ideological Self-Identification \nand Domain-Specific Issue Positions",
  #        subtitle = "Data: ANES cumulative file, matched partisans from the 1984 and 2020 cross-sections. \nHTMA indicates 'haven't thought much about this.'") +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12, face = "bold")) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  theme(axis.text = element_text(size = 9)) + 
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9)) + 
  theme(panel.spacing = unit(2, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure16.pdf", width = 8.8, height = 12.5)
